#include "guid_page.h"
#include "ui_guid_page.h"

guid_page::guid_page(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::guid_page)
{
    ui->setupUi(this);
    ui->pushButton_ref1->setStyleSheet("color: white; background-color: #55AAFF");
    ui->pushButton_ref2->setStyleSheet("color: white; background-color: #55AAFF");
    ui->pushButton_ref3->setStyleSheet("color: white; background-color: #55AAFF");
    ui->pushButton_ref4->setStyleSheet("color: white; background-color: #55AAFF");
    ui->pushButton_ref5->setStyleSheet("color: white; background-color: #55AAFF");
    ui->pushButton_ref6->setStyleSheet("color: white; background-color: #55AAFF");
    ui->pushButton_generate->setStyleSheet("color: white; background-color: #55AAFF");

    ui->tableWidget_res->setColumnCount(7);
    ui->tableWidget_res->setRowCount(1);
    ui->tableWidget_res->setHorizontalHeaderItem(0,new QTableWidgetItem("因子"));
    ui->tableWidget_res->setHorizontalHeaderItem(1,new QTableWidgetItem("躯体化惊恐"));
    ui->tableWidget_res->setHorizontalHeaderItem(2,new QTableWidgetItem("广泛性焦虑"));
    ui->tableWidget_res->setHorizontalHeaderItem(3,new QTableWidgetItem("分离性焦虑"));
    ui->tableWidget_res->setHorizontalHeaderItem(4,new QTableWidgetItem("社交恐怖"));
    ui->tableWidget_res->setHorizontalHeaderItem(5,new QTableWidgetItem("学校恐怖"));
    ui->tableWidget_res->setHorizontalHeaderItem(6,new QTableWidgetItem("总分"));
    ui->tableWidget_res->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_res->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_res->verticalHeader()->setVisible(false);
    ui->tableWidget_res->setEditTriggers(QAbstractItemView::NoEditTriggers);
#if 1
    sql = QSqlDatabase::addDatabase("QSQLITE", "sixth");
    sql.setDatabaseName("child.db");
    bool open_or_not = sql.open();
    if(open_or_not == true){
        qDebug() << "数据库连接成功";
    }
    else{
        qDebug() << "数据库连接失败";
    }
    this->setWindowTitle("儿童焦虑指导意见");
    setAttribute(Qt::WA_QuitOnClose,false);
    connect(ui->pushButton_ref1, SIGNAL(clicked(bool)), this, SLOT(click1()));
    connect(ui->pushButton_ref2, SIGNAL(clicked(bool)), this, SLOT(click2()));
    connect(ui->pushButton_ref3, SIGNAL(clicked(bool)), this, SLOT(click3()));
    connect(ui->pushButton_ref4, SIGNAL(clicked(bool)), this, SLOT(click4()));
    connect(ui->pushButton_ref5, SIGNAL(clicked(bool)), this, SLOT(click5()));
    connect(ui->pushButton_ref6, SIGNAL(clicked(bool)), this, SLOT(click6()));
    connect(ui->pushButton_generate, SIGNAL(clicked(bool)), this, SLOT(click_report()));

#endif

}


void guid_page::display(int id){

    QSqlQuery query1(sql);
    QSqlQuery query2(sql);
    QString str1;
    QString str2;
    str1 = "SELECT * FROM tb_result WHERE child_id=" + QString::number(id) + " ORDER BY id DESC LIMIT 1;";
    str2 = "SELECT birth,name,sex,phone,grade FROM child_data WHERE id=" + QString::number(id);
    query1.exec(str1);
    query1.next();
    query2.exec(str2);
    query2.next();

    grade = QString::number(query2.value(4).toInt() + 1) + "年级";
    phone = query2.value(3).toString();

    out_id = query1.value(0).toString();
    ui->label_num->setText(out_id);

    date = query1.value(2).toString();
    ui->label_date->setText(date);

    ui->label_company->setText(query1.value(3).toString());

    name = query2.value(1).toString();
    ui->label_name->setText(name);

    if(query2.value(2).toInt() == 0)    sex = "女";
    else    sex = "男";
    ui->label_sex->setText(sex);

    birth = query2.value(0).toString();
    ui->label_birth->setText(birth);


    ui->tableWidget_res->setItem(0, 0, new QTableWidgetItem("得分"));
    for(int i = 0; i < 6; i++){
        num[i] = query1.value(i + 4).toInt();
    }
    for(int i = 0; i < 6; i++){
        ui->tableWidget_res->setItem(0, i + 1, new QTableWidgetItem(query1.value(i + 4).toString()));
    }
    if(query1.value(9).toInt() < 40){
        ui->textEdit_con->setText(tr("该儿童目前没有焦虑性情绪障碍可疑表现，请继续保持！"));
        subs = "该儿童目前没有焦虑性情绪障碍可疑表现，请继续保持！";
    }
    else{
        ui->textEdit_con->setText(tr("该儿童有焦虑性情绪障碍可疑表现\n建议结合临床症状和相关医学检查做进一步确诊与治疗。"));
        subs = "该儿童有焦虑性情绪障碍可疑表现\n建议结合临床症状和相关医学检查做进一步确诊与治疗。";
    }
    this->show();
}


void guid_page::click1(){
    ui->textEdit_non->append(tr("   不要刻意在别人面前掩饰自己的羞怯、紧张，社交过程中，把注意力多放在对方和交谈内容上面，不要过分关注自己的表现。"));
}
void guid_page::click2(){
     ui->textEdit_non->append(tr("  家长不过分关注孩子症状，不要紧盯孩子表现，对其焦虑症状不批评不责备，给孩子良好的家庭支持，促进孩子心身健康发展。"));
}
void guid_page::click3(){
     ui->textEdit_non->append(tr("  做好孩子的学校管理，创造有利于儿童的适应过程与环境，减轻儿童压力，增强自信。"));
}
void guid_page::click4(){
     ui->textEdit_non->append(tr("  做好日常生活管理，培养孩子良好的性格和行为习惯：如保证充足的睡眠、建立积极的生活兴趣。"));
}
void guid_page::click5(){
     ui->textEdit_non->append(tr("  多进行规律的有氧运动，可以每天进行户外活动。"));
}
void guid_page::click6(){
     ui->textEdit_non->append(tr("  适当的放松训练，多让孩子参加文娱、体育活动，缓解孩子的心理恐惧。"));
}
void guid_page::click_report(){
#if 1
    QFile* file = new QFile("res.pdf");
    QFile* file_com = new QFile("com.txt");
    file->open(QIODevice::WriteOnly);
    file_com->open(QIODevice::ReadWrite);
    QTextStream  stream(file_com);
    QString com;
    stream >> com;

    QPdfWriter* pdf = new QPdfWriter(file);
    pdf->setTitle("结果报告");
    pdf->setPageSize(QPagedPaintDevice::A4);
    pdf->setPageMargins(QMargins(0, 0, 0, 0));
    pdf->setResolution(300);

    QPainter* painter = new QPainter(pdf);
    painter->setPen(Qt::black);
    painter->setBrush(Qt::black);
    painter->setFont(QFont("NSimSun", 20, QFont::Normal));
    painter->drawText(200, 200, QString::fromUtf8("儿童焦虑性情绪障碍筛查结果"));

    painter->drawRect(200, 250, 2000, 1);
    painter->drawRect(200, 700, 2000, 4);

    painter->setFont(QFont("NSimSun", 12, QFont::Normal));
    painter->drawText(1800, 330, com);

    painter->setFont(QFont("NSimSun", 14, QFont::Normal));

    painter->drawText(200, 450, QString::fromUtf8("患者ID ："));
    painter->drawText(450, 450, out_id);

    painter->drawText(700, 450, QString::fromUtf8("姓名 ："));
    painter->drawText(900, 450, name);

    painter->drawText(1200, 450, QString::fromUtf8("性别 ："));
    painter->drawText(1400, 450, sex);

    painter->drawText(1700, 450, QString::fromUtf8("年级 ："));
    painter->drawText(1900, 450, grade);

    painter->drawText(200, 550, QString::fromUtf8("电话 ："));
    painter->drawText(400, 550, phone);

    painter->drawText(900, 550, QString::fromUtf8("生日 ："));
    painter->drawText(1100, 550, birth);

    painter->drawText(1600, 550, QString::fromUtf8("日期 ："));
    painter->drawText(1800, 550, date);


    painter->drawText(200, 680, QString::fromUtf8("分数："));

    painter->drawText(200, 800, QString::fromUtf8("躯体化惊恐"));
    painter->drawText(600, 800, QString::fromUtf8("广泛性焦虑"));
    painter->drawText(1000, 800, QString::fromUtf8("分离性焦虑"));
    painter->drawText(1400, 800, QString::fromUtf8("社交恐怖"));
    painter->drawText(1750, 800, QString::fromUtf8("学校恐怖"));
    painter->drawText(2100, 800, QString::fromUtf8("总分"));

    painter->drawRect(200, 850, 2000, 1);
    painter->drawText(200, 940, QString::number(num[0]));
    painter->drawText(600, 940, QString::number(num[1]));
    painter->drawText(1000, 940, QString::number(num[2]));
    painter->drawText(1400, 940, QString::number(num[3]));
    painter->drawText(1750, 940, QString::number(num[4]));
    painter->drawText(2100, 940, QString::number(num[5]));
    painter->drawRect(200, 970, 2000, 4);

    painter->drawText(200, 1400, QString::fromUtf8("建议："));
    painter->drawRect(200, 1450, 2000, 4);
    painter->drawText(200, 1550, subs);

    painter->drawRect(200, 3000, 2000, 4);
    painter->drawText(1800, 3100, QString::fromUtf8("医师签字："));
    painter->drawText(1800, 3200, QString::fromUtf8("   年   月   日"));

    QMessageBox::information(this, "PDF生成成功", "PDF导出成功", "确认", "取消");

    delete painter;
    delete pdf;
    file->close();
#endif
}










guid_page::~guid_page()
{
    delete ui;
}
